/*
* motor.c
*
* Created: 2012/6/15 20:44:49
*  Author: albert
*/

#include "motor.h"
#include "atmega128_usart.h"

volatile unsigned  char pre_motor_mode=0;
volatile unsigned  char current_motor_mode=0;
volatile unsigned  int timer_counter=0;
volatile unsigned  int spi_counter=0;

unsigned char motor_usart_num = 0;
unsigned char motor_usart_speed = BAUDRATE_38400; //0->9600db 1->38400db

//only has AT and CMD, not parameters
struct Compact_CMD
{
	unsigned char firm_ver[1];
	unsigned char error_byte[1];
	unsigned char get_config_para[1];
	unsigned char set_config_para[1];
	unsigned char motor0_coast[1];
	unsigned char motor1_coast[1];
	//speed range from 0 to 127
	unsigned char motor0_forward[2]; //default speed is 0
	unsigned char motor0_forward_128[2]; //default speed is 128
	unsigned char motor0_reverse[2]; //default speed is 0
	unsigned char motor0_reverse_128[2]; //default speed is 128
	unsigned char motor1_forward[2]; //default speed is 0
	unsigned char motor1_forward_128[2]; //default speed is 128
	unsigned char motor1_reverse[2]; //default speed is 0
	unsigned char motor1_reverse_128[2]; //default speed is 128
} motor_cmd =
{
	{
		0x81
	}
	,
	{
		0x82
	}
	,
	{
		0x83
	}
	,
	{
		0x84
	}
	,
	{
		0x86
	}
	,
	{
		0x87
	}
	,
	{
		0x88,0x00
	}
	,
	{
		0x89,0x00
	}
	,
	{
		0x8A,0x00
	}
	,
	{
		0x8B,0x00
	}
	,
	{
		0x8C,0x00
	}
	,
	{
		0x8D,0x00
	}
	,
	{
		0x8E,0x00
	}
	,
	{
		0x8F,0x00
	}
};


void go_reverse(void){
	//set the motor speed
	motor_cmd.motor1_reverse[1]=0x60;
	USART_BYTES_Transmit(motor_usart_num, motor_cmd.motor1_reverse,sizeof(motor_cmd.motor1_reverse));
	
	/*motor_cmd.motor1_reverse[1]=0x00;
	USART_BYTES_Transmit(motor_usart_num, motor_cmd.motor1_reverse,sizeof(motor_cmd.motor1_reverse));
	*/
}

void go_forward(void){
	motor_cmd.motor1_forward[1]=0x60;
	USART_BYTES_Transmit(motor_usart_num, motor_cmd.motor1_forward,sizeof(motor_cmd.motor1_forward));
}

void turn_left(void){
	motor_cmd.motor0_forward[1]=0x7f;
	USART_BYTES_Transmit(motor_usart_num, motor_cmd.motor0_forward,sizeof(motor_cmd.motor0_forward));
}

void turn_right(void){
	motor_cmd.motor0_reverse[1]=0x7f;
	USART_BYTES_Transmit(motor_usart_num, motor_cmd.motor0_reverse,sizeof(motor_cmd.motor0_reverse));
}

void stop_motor(void) {
	motor_cmd.motor1_forward[1]=0x00;
	USART_BYTES_Transmit(motor_usart_num, motor_cmd.motor1_forward,sizeof(motor_cmd.motor1_forward));
}

